BottomFloatPanel=function(){
	this.option=null;
	this.inited=false;
	this.popFlagClass='panel-poped';
}

BottomFloatPanel.tmpHtml=''
+'<div class="xq-float-panel">'
	+'<div class="xq-header-bar">'
		+'<ul class="xq-tab-bar">'
			//<li class="xq-tab-item" info-id='detail-shangxi'>赏析</li>
		+'</ul>'
	+'</div>'
	+'<div class="xq-panel-content select-able">'
		//<div class="xq-padding-content" id="detail-zhushi"" show-name=""remark">暂无数据</div>
	+'</div>'
+'</div>';

BottomFloatPanel.defVal={
	panelId:null,
	tabItemList:[],//{inforId:'001',itemName:'itemName',contentData:'',showName:''}
};

BottomFloatPanel.prototype.initPanel = function(opt) {
	if(this.inited){
		return;
	}
	this.option=$.extend(true,{},BottomFloatPanel.defVal,opt);
	this.option.panelId=this.option.panelId || ('bfp-'+new Date().getTime());
	var tplClone=$(BottomFloatPanel.tmpHtml).clone();
	$(tplClone).attr('id',this.option.panelId);
	$('body').append(tplClone);
	//
	this._initPanelHeight();
	//
	this._initTabItems();
	//
	var that=this;
	$("#"+this.option.panelId+" .xq-tab-bar").on('tap','.xq-tab-item',function(e){
		if(that.option.beforeHandleItemTab){
			that.option.beforeHandleItemTab({panelIns:that,tabEle:this});
		}
		that._initTabItemEvent(this);
		if(that.option.afterHandleItemTab){
			that.option.afterHandleItemTab({panelIns:that,tabEle:this});
		}
	});
	/* $("#"+this.option.panelId+" .xq-tab-bar").on('swipedown',function(e){
		that.popDownPanel();
	});
	$("#"+this.option.panelId+" .xq-tab-bar").on('swipeup',function(e){
		that.popUpPanel();
	}); */
	$("#"+this.option.panelId+" .xq-tab-bar").on('dragstart',function(e){
		that.dragStartY=e.detail.center.y;
		var floatPanelSel="#"+that.option.panelId;
		that.startTop=$(floatPanelSel).position().top;
		console.info('dragStartY='+that.dragStartY);
	});
	$("#"+this.option.panelId+" .xq-tab-bar").on('drag',function(e){
		var dragCurrentY=e.detail.center.y;
		var detY=dragCurrentY-that.dragStartY;
		//
		var popFlagClass=that.popFlagClass;
		var floatPanelSel="#"+that.option.panelId;
		var currentTop=that.startTop+detY;
		if(currentTop<that.closedTop && currentTop>that.popedTop){
			$(floatPanelSel).css('top',currentTop+'px');
		}
	});
	$("#"+this.option.panelId+" .xq-tab-bar").on('dragend',function(e){
		var dragEndY=e.detail.center.y;
		if(dragEndY<(that.popedTop-45) || dragEndY>that.closedTop){
			dragEndY=that.dragStartY;
		}
		var detY=dragEndY-that.dragStartY;
		var absDetY=Math.abs(detY);
		if(absDetY==0){
			return;
		}
		var popFlagClass=that.popFlagClass;
		var floatPanelSel="#"+that.option.panelId;
		if(absDetY<that.contentH/4){
			if($(floatPanelSel).is('.'+popFlagClass)){
				that.popUpPanel();
			}else{
				that.popDownPanel();
			}
		}else{
			if($(floatPanelSel).is('.'+popFlagClass)){
				that.popDownPanel();
			}else{
				that.popUpPanel();
			}
		}
	});
	//
	$("#"+this.option.panelId).on('swipeleft','.xq-fill-inner-content',function(e){
		var preNextTabItem=that._findPreNextTabItem();
		if(preNextTabItem.next){
			$(preNextTabItem.next).trigger('tap');
		}
	});
	$("#"+this.option.panelId).on('swiperight','.xq-fill-inner-content',function(e){
		var preNextTabItem=that._findPreNextTabItem();
		if(preNextTabItem.pre){
			$(preNextTabItem.pre).trigger('tap');
		}
	});
	//返回事件监听
	var oldBack=mui.back;
	mui.back = function(){
		var popFlagClass=that.popFlagClass;
		var floatPanelSel="#"+that.option.panelId;
		if($(floatPanelSel).is('.'+popFlagClass)){
			that.popDownPanel();
			return false;  //禁用物理返回键  也可以写其他逻辑
		}
		return oldBack();
	}
	//
	this.inited=true;
};

BottomFloatPanel.prototype._initPanelHeight=function(){
	var winH=$(window).height();
	this.panelH=winH*0.65;
	this.closedTop=winH-45;
	this.contentH=this.panelH-45;
	this.popedTop=winH-this.panelH;
	var floatPanelSel="#"+this.option.panelId;
	//$(floatPanelSel).find('.xq-panel-content').css('height',contentH+'px');
	$(floatPanelSel).css('height',this.panelH+'px');
	$(floatPanelSel).css('top',this.closedTop+'px');
	//
}

BottomFloatPanel.prototype._initTabItems=function(){
	var that=this;
	var floatPanelSel="#"+this.option.panelId;
	var tabItemList=this.option.tabItemList;
	if(tabItemList && tabItemList.length>0){
		var winW=$(window).width();
		var tabBar=$(floatPanelSel).find('.xq-tab-bar');
		var panelContent=$(floatPanelSel).find('.xq-panel-content');
		var panelWidth=winW*tabItemList.length;
		$(panelContent).css('width',panelWidth+'px');
		mui.each(tabItemList,function(index,itemValue){
			var activeClass='';
			if(index==0){
				activeClass=' xq-item-active';
			}
			var itemTpl='<li class="xq-tab-item'+activeClass+'" info-id="'+itemValue.inforId+'">'+itemValue.itemName+'</li>';
			$(tabBar).append(itemTpl);
			//
			var contentTpl=''
			+'<div class="xq-float-panel-item'+activeClass+'">'
				+'<div class="xq-fill-inner-content" id="'+itemValue.inforId+'">暂无数据</div>'
			+'</div>';
			var $contentTpl=$(contentTpl);
			$contentTpl.css('height',that.contentH+'px');
			$contentTpl.css('width',winW+'px');
			$contentTpl.find('.xq-fill-inner-content').css('min-height',that.contentH+'px');
			if(itemValue.showName){
				$contentTpl.find('.xq-fill-inner-content').attr('show-name',itemValue.showName);
			}
			if(itemValue.contentData){
				$contentTp.find('.xq-fill-inner-content').html(itemValue.contentData);
			}
			$(panelContent).append($contentTpl);
		});
		$(panelContent).on('swipedown','.xq-float-panel-item',function(){
			var scrollTop=$(this).closest('.xq-float-panel-item').scrollTop();
			if(scrollTop==0){
				that.popDownPanel();
			}
		});
	}
}

BottomFloatPanel.prototype.popUpPanel=function(){
	var that=this;
	var popFlagClass=this.popFlagClass;
	var floatPanelSel="#"+this.option.panelId;
	if(!$(floatPanelSel).is('.'+popFlagClass)){
		/* var winH=$(window).height();
		var panelH=$(floatPanelSel).height(); */
		$(floatPanelSel).addClass(popFlagClass);
		this._addBgMask(11,function(eleId){
			that.popDownPanel();
		});
	}
	var panelTop=that.popedTop;
	$(floatPanelSel).css('top',panelTop+'px');
}

BottomFloatPanel.prototype.popDownPanel=function(){
	var eleId=this.option.maskEleId;
	var winH=$(window).height();
	var closePanelH=winH-45;
	var popFlagClass='panel-poped';
	var floatPanelSel="#"+this.option.panelId;
	$(floatPanelSel).css('top',closePanelH+'px');
	$(floatPanelSel).removeClass(popFlagClass);
	$('#'+eleId).remove()
}

BottomFloatPanel.prototype.fillPanelData=function(data){
	fillDataToTpl("#"+this.option.panelId,data);
}

BottomFloatPanel.prototype.resetInnerContentTop=function(){
	var floatPanelSel="#"+this.option.panelId;
	var innerPanel = $(floatPanelSel).find('.xq-fill-inner-content');
	innerPanel.each(function(){
	    this.scrollIntoView();
	  });
}

BottomFloatPanel.prototype.resetFirstTabAndInnerContentTop=function(){
	this.switchTabAndPanel("[info-id='detail-zhushi']");
	this.resetInnerContentTop();
}

BottomFloatPanel.prototype._initTabItemEvent=function(tabedEle){
	this.popUpPanel();
	//
	this.switchTabAndPanel(tabedEle);
}

BottomFloatPanel.prototype.switchTabAndPanel=function(tabedEle){
	//
	var floatPanelSel="#"+this.option.panelId;
	$(floatPanelSel).find('.xq-tab-item').removeClass('xq-item-active');
	$(tabedEle).closest('.xq-tab-item').addClass('xq-item-active');
	//
	$(floatPanelSel).find('.xq-panel-content .xq-float-panel-item').removeClass('xq-item-active');
	var infoId=$(tabedEle).closest('.xq-tab-item').attr('info-id');
	$('#'+infoId).closest('.xq-float-panel-item').addClass('xq-item-active');
	//
	var panelItems=$(floatPanelSel).find('.xq-panel-content .xq-float-panel-item');
	var actIndex=0;
	for(var i=0;i<panelItems.length;i++){
		if($(panelItems[i]).is('.xq-item-active')){
			actIndex=i;
			break;
		}
	}
	var winW=$(window).width();
	var leftPos=0-(winW*actIndex);
	$(floatPanelSel).find('.xq-panel-content').css('left',leftPos+'px');
}
		
BottomFloatPanel.prototype._addBgMask=function(zIndex,tapFunction){
	var eleId='bg-mask-'+new Date().getTime();
	this.option.maskEleId=eleId;
	var maskEle='<div class="xq-bg-mask" id="'+eleId+'"></div>';
	
	$('body').append($(maskEle));
	$('#'+eleId).css('z-index',zIndex);
	if(tapFunction){
		$('#'+eleId).off('tap').on('tap',function(e){
			e.stopPropagation();
			setTimeout(function(){
				tapFunction(eleId);
			},10)
			
		});
	}
}

BottomFloatPanel.prototype._findPreNextTabItem=function(){
	var preAndNextTabItem={};
	var $ele=$("#"+this.option.panelId +" .xq-tab-bar").find(".xq-tab-item");
	var nowActIdx=0;
	for(var idx=0;idx<$ele.length;idx++){
		if($($ele[idx]).is('.xq-item-active')){
			nowActIdx=idx;
			break;
		}
	}
	if(nowActIdx!=0){
		preAndNextTabItem['pre']=$ele[idx-1];
	}
	if(nowActIdx+1<$ele.length){
		preAndNextTabItem['next']=$ele[idx+1];
	}
	return preAndNextTabItem;
}